/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "IShaderEffect.h"
#include "IRenderer.h"

IShaderEffect *IShaderEffect::s_pGlobalShader = NULL;
std::map<std::string, sEffectData> IShaderEffect::m_lRegisteredData;

IShaderEffect *IShaderEffect::GetGlobal() 
{ 
	return s_pGlobalShader; 
}

IShaderEffect::IShaderEffect(void)
:	bIsLoaded(false), 
	uiNumPasses(1)
{
	pRenderer = coreGetRenderer();
}

IShaderEffect::~IShaderEffect(void)
{
}

int IShaderEffect::getNumPasses()
{
	return uiNumPasses;
}

bool IShaderEffect::isLoaded() const
{
	return bIsLoaded;
}

bool IShaderEffect::bindGlobal()
{
	if ( bind() ) {
		s_pGlobalShader = this;
		return true;
	}
	return false;
}

void IShaderEffect::RegisterHandle(const std::string &identifier, eEffectDataType dataType, unsigned int numElements, void* data)
{
	sEffectData ed;
	ed.dataType = dataType;
	ed.numElements = numElements;
	ed.data = data;
	m_lRegisteredData[identifier] = ed;
}

void IShaderEffect::checkRegisteredHandles()
{
	SHADERHANDLE handle;
	for (registereddata_list::iterator it=m_lRegisteredData.begin(); it!=m_lRegisteredData.end(); ++it)
	{
		if (hasParameter(it->first)) {
			handle = getParameterByName(it->first);		
			switch (it->second.dataType) {
				case TYPE_TEXTURE:
					setTexture(handle, (ITexture*)it->second.data);
					break;
				default:
					break;
			}
		}
	}
}